package com.project.action;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import java.util.*;
import java.text.SimpleDateFormat;
import com.project.dao.implement.AccountCurveDataImpl;
import com.project.bean.AccountCurveData;
import org.json.*;

public class AccountCurveDataServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public AccountCurveDataServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html/json;charset=utf-8");
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        String userBankAccount = request.getParameter("userBankAccount");
        String strBeginTime = request.getParameter("beginTime");
        String strEndTime = request.getParameter("endTime");
        String strIntervalTime = request.getParameter("intervalTime");
        String mode = request.getParameter("type");
        
        JSONObject json = new JSONObject(); 
        JSONArray jsonArray =new JSONArray();
		JSONObject member = null; 
        
        try{
	        SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd");
	        Date dateBeginTime = a.parse(strBeginTime);
	        Date dateEndTime = a.parse(strEndTime);
	        int iIntervalTime = Integer.parseInt(strIntervalTime);
	        
	        String tempBTime = "";
	        String tempETime = "";
	        tempBTime = (new SimpleDateFormat("yyyyMMdd")).format(dateBeginTime);
	        tempETime = (new SimpleDateFormat("yyyyMMdd")).format(dateEndTime);
	        Map<String,Object> param = new HashMap<String,Object>();
	        param.put("@userAccount", userBankAccount);
	        param.put("@beginTime", tempBTime);
	        param.put("@endTime", tempETime);
	        param.put("@interval", iIntervalTime);
	        
	        AccountCurveDataImpl mapper = new AccountCurveDataImpl();
	        List<AccountCurveData> resultList = null;
	        int iMode = 0;
	        if (mode.equals("根据流入流出资金量统计"))
	        {
	        	resultList = mapper.GetFund(param);
	        	iMode = 1;
	        }
	        else
	        {
	        	resultList = mapper.GetCount(param);
	        	iMode = 2;
	        }
	         
	        /*{
    		member = new JSONObject();
			member.put("fund", 200000);
			member.put("timeBlock", "2011/01/11<br>-2011/01/21");
			member.put("tag", 1);
			jsonArray.put(member);
	        }
			
	        {
			member = new JSONObject();
			member.put("fund", 100000);
			member.put("timeBlock", "2011/01/11<br>-2011/01/21");
			member.put("tag", 0);
			jsonArray.put(member);
	        }
			
	        {
			member = new JSONObject();
			member.put("fund", 500000);
			member.put("timeBlock", "2011/01/12<br>-2011/01/22");
			member.put("tag", 1);
			jsonArray.put(member);
	        }
			
	        {
			member = new JSONObject();
			member.put("fund", 700000);
			member.put("timeBlock", "2011/01/12<br/>-2011/01/22");
			member.put("tag", 0);
			jsonArray.put(member);
	        }
			
	        {
			member = new JSONObject();
			member.put("fund", 100000);
			member.put("timeBlock", "2011/01/23<br/>-2011/02/03");
			member.put("tag", 1);
			jsonArray.put(member);
	        }
			
	        {
			member = new JSONObject();
			member.put("fund", 300000);
			member.put("timeBlock", "2011/01/23<br/>-2011/02/03");
			member.put("tag", 0);
			jsonArray.put(member);
	        }
			
	        {
			member = new JSONObject();
			member.put("fund", 150000);
			member.put("timeBlock", "2011/02/04<br/>-2011/02/14");
			member.put("tag", 1);
			jsonArray.put(member);
	        }
			
	        {
			member = new JSONObject();
			member.put("fund", 350000);
			member.put("timeBlock", "2011/02/04<br/>-2011/02/14");
			member.put("tag", 0);
			jsonArray.put(member);
	        }
			
	        {
			member = new JSONObject();
			member.put("fund", 850000);
			member.put("timeBlock", "2011/02/15<br/>-2011/03/01");
			member.put("tag", 1);
			jsonArray.put(member);
	        }
			
	        {
			member = new JSONObject();
			member.put("fund", 50000);
			member.put("timeBlock", "2011/02/15<br/>-2011/03/01");
			member.put("tag", 0);
			jsonArray.put(member);
	        }*/
			
	        
    		for(AccountCurveData item: resultList){
    			if (item == null)
        		{
        			continue;
        		}
    			member = new JSONObject();
    			if (iMode == 1)
    			{
    				member.put("value", item.GetValue());
    			}
    			else if (iMode == 2)
    			{
    				int fund = (int)item.GetValue();
    				member.put("value", fund);
    			}
    			member.put("timeBlock", item.GetTimeBlock());
    			member.put("tag", item.GetTag());
    			jsonArray.put(member);
    		}
    		json.put("jsonArray", jsonArray);
    		json.put("mode", iMode);
        }
        catch (Exception e) {   
            return;   
        } 
        
        /*try{
	        SimpleDateFormat a=new SimpleDateFormat("yyyy-MM-dd");
	        Date dateBeginTime = a.parse(strBeginTime);
	        Date dateEndTime = a.parse(strEndTime);
	        int iIntervalTime = Integer.paresInt(strIntervalTime);
	        
	        Calendar cd = Calendar.getInstance(); 
	        cd.setTime(dateBeginTime);
	        Date intervalTime = cd.getTime();
	        int i = 1;
	        AccountCurveDataImpl mapper = new AccountCurveDataImpl();
	        float fund = 0;
	        String tempBTime = "";
	        String tempETime = "";
	        Map<String,Object> param = null;
	        JSONObject json = new JSONObject(); 
	        JSONArray jsonArray =new JSONArray();
    		JSONObject member = null; 
	        while (dateBeginTime < dateEndTime)
	        {
		        cd.setTime(dateBeginTime);   
		        cd.add(Calendar.DATE, iIntervalTime * i); 
		        intervalTime = cd.getTime();
		        tempBTime = (new SimpleDateFormat("yyyyMMdd")).format(dateBeginTime);
		        tempETime = (new SimpleDateFormat("yyyyMMdd")).format(intervalTime);
		        param = new HashMap<String,Object>();
		        param.put("userAccount", userBankAccount);
		        param.put("beginTime", tempBTime);
		        param.put("endTime", tempETime);
		        fund = mapper.GetFund(param);
		        tempBTime = (new SimpleDateFormat("yyyy/MM/dd")).format(dateBeginTime);
		        tempETime = (new SimpleDateFormat("yyyy/MM/dd")).format(intervalTime);
		        member = new JSONObject();
		        member.put("fund", fund);
		        member.put("interval",tempBTime + "-" + tempETime);
		        jsonArray.put(member);
		        dateBeginTime = intervalTime;
		        ++i;
	        }
	        
	        cd.setTime(dateBeginTime);   
	        cd.add(Calendar.DATE, iIntervalTime * i); 
	        tempBTime = (new SimpleDateFormat("yyyy/MM/dd")).format(dateBeginTime);
	        tempETime = (new SimpleDateFormat("yyyy/MM/dd")).format(dateEndTime);
	        param = new HashMap<String,Object>();
	        param.put("userAccount", userBankAccount);
	        param.put("beginTime", tempBTime);
	        param.put("endTime", tempETime);
	        fund = mapper.GetFund(param);
	        tempBTime = (new SimpleDateFormat("yyyyMMdd")).format(dateBeginTime);
	        tempETime = (new SimpleDateFormat("yyyyMMdd")).format(dateEndTime);
	        member = new JSONObject();
	        member.put("fund", fund);
	        member.put("interval",tempBTime + "-" + tempETime);
	        jsonArray.put(member);
	        
	        json.put("jsonArray", jsonArray);
        }
        catch (Exception e) {   
            return null;   
        } */ 
        
        response.getWriter().write(json.toString());  
    	response.getWriter().close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
